<template>
	<div class="page-container padding-20 query-list-container">
		<div class="search-container">
			<el-form :inline="true" ref="form" size="small">
				<el-form-item label="统计时间">
					<el-date-picker
						v-model="rangeDate"
						type="daterange"
						align="right"
						range-separator="至"
						start-placeholder="开始日期"
						end-placeholder="结束日期"
						:picker-options="pickerOptions"
					></el-date-picker>
				</el-form-item>
				<el-button type="primary" @click="onSearch" size="small">查询</el-button>
				<el-button @click="onReset" size="small">重置</el-button>
			</el-form>
		</div>
		<el-table class="mt-20 el-table-modify" :data="tableData" border show-summary>
			<el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
			<el-table-column prop="date" align="center" label="日期">
				<template slot-scope="scope">
					{{scope.row.date | dateFrm}}
				</template>
			</el-table-column>
			<el-table-column prop="channelName" align="center" label="渠道商名称"></el-table-column>
			<el-table-column prop="brandName" align="center" label="权益品牌"></el-table-column>
			<el-table-column prop="productName" align="center" label="产品名称"></el-table-column>
			<el-table-column prop="saleAmount" align="center" label="新增销售"></el-table-column>
			<el-table-column prop="renewAmount" align="center" label="新增续费"></el-table-column>
		</el-table>
		<el-pagination
			ref="pagination"
			class="mt-20"
			background
			:page-size.sync="pageOption.pageSize"
			:total="pageOption.total"
			@size-change="onSizeChange"
			@current-change="onPageChange"
			layout="slot,->,prev,pager,next,sizes,jumper"
		>
			<span class="pagination-title">
				共 {{ pageOption.total }} 条记录 第 {{ pageOption.page }} /
				{{ pageOption.pageTotal }} 页
			</span>
		</el-pagination>
	</div>
</template>

<script>
import { privilegeChannelCount } from "@/api/report";
import { pickerOptions, momentFormatStr } from "@/utils/const";
import moment from "moment";

export default {
	name: "ChannelSaleEquity",
	data() {
		this.pickerOptions = pickerOptions;
		return {
			rangeDate: [new Date(), new Date()],
			tableData: [],
			pageOption: {
				page: 1,
				pageSize: 20,
				total: 0,
				pageTotal: 1
			}
		};
	},
	methods: {
		onSearch() {
			this.pageOption.page = 1;
			this.requestData();
		},
		onReset() {
			this.pageOption.page = 1;
			this.rangeDate = [new Date(), new Date()];
			this.requestData();
		},
		async requestData() {
			const { page, pageSize: size } = this.pageOption,
				params = { page: page - 1, size };
			if (this.rangeDate[0]) {
				params.start = moment(this.rangeDate[0]).format(
					momentFormatStr.date
				);
				params.end = moment(this.rangeDate[1]).format(momentFormatStr.date);
			}
			const { data, total } = await privilegeChannelCount(params);
			this.tableData = data;
			this.pageOption.total = total;
			this.pageOption.pageTotal = total ? Math.ceil(total / size) : 1;

			if (this.$refs.pagination) {
				let childrenRefs = this.$refs.pagination.$children;
				this.$nextTick(() => {
					childrenRefs.forEach(child => child.$forceUpdate());
				});
			}
		},
		onSizeChange(size) {
			this.pageOption.pageSize = size;
			this.requestData();
		},
		onPageChange(page) {
			this.pageOption.page = page;
			this.requestData();
		}
	},
	created() {
		this.requestData();
	}
};
</script>

<style lang="scss">
</style>
